package com.open.pokergames.texas.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.pokergames.R;
import com.open.pokergames.texas.repository.entity.RoomEntity;
import com.sinothk.android.utils.XUtils;
import com.sinothk.android.utils.inters.OnSuperListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class RoomListAdapter extends RecyclerView.Adapter<RoomListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<RoomEntity> mData;
    private Context mContext;
    private OnSuperListener<RoomEntity> superListener;

    public RoomListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<RoomEntity> superListener) {
        this.superListener = superListener;
    }

    public void setData(List<RoomEntity> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<RoomEntity> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.room_adapter_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final RoomEntity itemData = mData.get(position);
        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getRoomName()));
        holder.roomManagerTv.setText("房主：" + XUtils.string().getNotNullValue(itemData.getRoomLeader()));

        if (itemData.getRoomType() == 1) {
            holder.roomTypeTv.setText("五张德州");
            holder.roomTypeTv.setBackgroundResource(R.drawable.shape_room_item_r_02);
        } else {
            holder.roomTypeTv.setText("德州扑克");
            holder.roomTypeTv.setBackgroundResource(R.drawable.shape_room_item_r_0);
        }

        if (itemData.getState() == 0) { // 类型：0:待入座 1:游戏中 2:已解散
            holder.stateTv.setText("入座中");
        } else if (itemData.getState() == 1) {
            holder.stateTv.setText("游戏中");
        } else if (itemData.getState() == 2) {
            holder.stateTv.setText("已解散");
        } else {
            holder.stateTv.setText("异常");
        }

        if (superListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    superListener.onClick(position, itemData, "");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView titleTv, timeTv, roomManagerTv, roomTypeTv, stateTv;
//        RoundedImageView imageView;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            roomManagerTv = itemView.findViewById(R.id.roomManagerTv);
            timeTv = itemView.findViewById(R.id.timeTv);
            roomTypeTv = itemView.findViewById(R.id.roomTypeTv);

            stateTv = itemView.findViewById(R.id.stateTv);

//            recNumTv = itemView.findViewById(R.id.recNumTv);
//            perNumTv = itemView.findViewById(R.id.perNumTv);
//            recDaysTv = itemView.findViewById(R.id.recDaysTv);
//
//            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}